#include "File.h"
#include <ArduinoJson.h>

String WIFI_STA_SID_KEY = "WIFI_STA_SID_KEY";
String WIFI_STA_SID_PASS_KEY = "WIFI_STA_SID_PASS_KEY";
String WATHER_KEY_CONF = "WATHER_KEY";
String WATHER_LOCATION_CONF = "WATHER_LOCATION";

struct UserConf {
  String sid;
  String pass;
  String wather_key;
  String localation;
} conf;

UserConf getUserConf() {
  return conf;
}
void setUserConf(UserConf uc) {
  conf = uc;
}
//加载json模式配置数据
boolean  loadJsonConf(String file_name) {
  Serial.println("开始加载配置数据：" + file_name);
  String jsonconf = loadFile(file_name);
  if (jsonconf == "")
  {
    Serial.println("配置数据为空");
    return false;
  }
  Serial.println("获取配置数据:" + jsonconf);
  StaticJsonDocument<256> doc;
  deserializeJson(doc, jsonconf);
  String sid = doc[WIFI_STA_SID_KEY];
  if (sid == "" || sid == "null")
  {
    doc.clear();
    return false;
  }
  conf.sid = sid;
  String sidpass = doc[WIFI_STA_SID_PASS_KEY];
  conf.pass = sidpass;
  String wk = doc[WATHER_KEY_CONF];
  conf.wather_key = wk;
  String wl = doc[WATHER_LOCATION_CONF];
  if (wl == "" || wl == "null")
  {
    wl = "beijing";
  }
  conf.localation =  wl;
  doc.clear();
  return true;
}

//保存 stat模式配置数据
boolean  saveJsonConf( String fileName)
{
  StaticJsonDocument<256> doc;
  doc[WIFI_STA_SID_KEY] = conf.sid;
  doc[WIFI_STA_SID_PASS_KEY] = conf.pass;
  doc[WATHER_KEY_CONF] = conf.wather_key;
  doc[WATHER_LOCATION_CONF] = conf.localation;
  String wificonf;
  serializeJson(doc, wificonf);
  Serial.println ("[SAVE]>>" + wificonf);
  writesave(fileName, wificonf);
  doc.clear();
  return true;
}
